package com.athena.web.controller.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.athena.api.user.IUserService;
import com.athena.dal.dao.GenericDao;
import com.athena.dal.entities.Faculty;
import com.athena.dal.entities.Student;
import com.athena.dal.entities.User;

public class UserDetailServiceImpl implements UserDetailsService {

	@Autowired
	IUserService athenaUserService;
	
	@Autowired 
	private GenericDao genericDao;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		
	    User user =	athenaUserService.getUser(username);
		if (user != null) {
			AthenaUser athenaUser = new AthenaUser();
			if(1 == user.getRole() ){
				athenaUser.setUser(genericDao.findById(Student.class, user.getId()));
			}else if(2 == user.getRole() ){
				athenaUser.setUser(genericDao.findById(Faculty.class, user.getId()));
			}
			return athenaUser;
		}
	    throw new UsernameNotFoundException("no user found for user name ="+username);
	}

}
